using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.SharePoint;
using SharePoint.DataMapper;

namespace SharePoint.DataMapper
{
	[FieldMapper(typeof(SPFieldChoice), SPFieldType.Choice)]
	[FieldMapper(typeof(SPFieldModStat), SPFieldType.ModStat)]
	public class FieldChoiceMapper : FieldMultiChoiceMapper
	{
		protected override SPField AddFieldInternal(ListDataContext context, SPFieldCollection collection, Type propertyType, FieldAttribute attribute)
		{
			SPFieldChoice field = (SPFieldChoice)base.AddFieldInternal(context, collection, propertyType, attribute);
			field.EditFormat = attribute.ChoiceFormat;
			return field;
		}

		protected override FieldAttribute GetFieldAttributeInternal(SPField field)
		{
			FieldAttribute attribute = base.GetFieldAttributeInternal(field);
			attribute.ChoiceFormat = ((SPFieldChoice) field).EditFormat;
			return attribute;
		}

		protected override object SetFieldInternal(ListDataContext context, object value, Type propertyType)
		{
			FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
			return Helpers.GetChoiceValue(fieldInfo);
		}

		protected override object GetFieldInternal(ListDataContext context, object value, Type propertyType)
		{
			Dictionary<string, string> values = Helpers.GetEnumValues(propertyType);
			return Enum.Parse(propertyType, values[value.ToString()]);
		}
	}
}